package com.bwda.dsrs.systemservice.controller;


import com.bwda.dsrs.base.centerbase.response.CentreCutPageResponse;
import com.bwda.dsrs.base.centerbase.response.ResponseEntity;
import com.bwda.dsrs.base.util.CopyUtil;
import com.bwda.dsrs.systemservice.aspect.LogTypeEnum;
import com.bwda.dsrs.systemservice.aspect.Loggable;
import com.bwda.dsrs.systemservice.domain.condition.adminlog.AdminLogCondition;
import com.bwda.dsrs.systemservice.domain.exception.SystemServiceException;
import com.bwda.dsrs.systemservice.domain.form.adminlog.AdminLogQueryForm;
import com.bwda.dsrs.systemservice.domain.po.adminlog.AdminLogPo;
import com.bwda.dsrs.systemservice.domain.vo.adminlog.AdminLogVo;
import com.bwda.dsrs.systemservice.service.LogAdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wj
 * @date 2020/10/17
 */
@RestController
@RequestMapping("/s/logadmin")
@Api(value = "/s/logadmin", description = "管理员日志表")
public class LogAdminController extends BaseController {

	@Autowired
	private LogAdminService logAdminService;


	@ApiOperation(value = "查询管理员日志表列表(带分页)",notes = "查询管理员日志表列表(带分页)",httpMethod = "GET")
	@Loggable(descp = "查询管理员日志表列表(带分页)", type = LogTypeEnum.QUERY, include = "")
	@RequestMapping(value = "/queryPageList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreCutPageResponse<AdminLogVo>> queryPageList(@ModelAttribute@Valid AdminLogQueryForm form) throws SystemServiceException {
		AdminLogCondition condition = this.getConditionByQueryForm(form);
		List<AdminLogVo> voList = new ArrayList<>();
		int count = logAdminService.queryCount(condition);
		if (count > 0) {
			List<AdminLogPo> poList = logAdminService.queryList(condition);
			voList = CopyUtil.transfer(poList, AdminLogVo.class);
		}
		return getSuccessResult(getPageResponse(form, count, voList));
	}


	/**
	 * LogAdminQueryForm转换为LogAdminCondition
	 *
	 * @param form
	 * @return
	 */
	private AdminLogCondition getConditionByQueryForm(AdminLogQueryForm form) {
		AdminLogCondition condition = CopyUtil.transfer(form, AdminLogCondition.class);
		return condition;
	}

	/*public static String getRandomString(int length){
		String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
		// update:注释-不安全的伪随机数，请使用SecureRandom random = new SecureRandom(); by:xuja
		Random random=new Random();
		StringBuffer sb=new StringBuffer();
		for(int i=0;i<length;i++){
			int number=random.nextInt(62);
			sb.append(str.charAt(number));
		}
		return sb.toString();
	}*/


}